"use strict";

const Controller = require("egg").Controller;
const { getCookieValue } = require("../../public/processCookies");
class DefaultController extends Controller {
  async ping() {
    console.log("进入ping方法");
    const { ctx, app } = this;
    const message = ctx.args[0];
    if (message.startsWith("/onurefresh")) {
      await this.fetchRegONU(message);
    }
    if (message.startsWith("/onufindlistrefresh")) {
      await this.fetchUnregONU(message);
    }
    await ctx.socket.emit("message", `Hi! I've got your message: ${message}`);
  }
  async fetchRegONU(message) {
    const { ctx, app } = this;
    let reg = /.*oltId=(.*)&tid=(.*)&tenant=(.*)/gi;
    let group = reg.exec(message);
    if (group) {
      const [url, oltId, tid,tenant] = group;
      let params = {
        oltId,
        tid,
      };
      ctx.tenant = tenant;
      await ctx.socket.emit("message",`查询fetchRegONU->${JSON.stringify(params)}`);
      if (params.oltId == "Any") {
        const result = await ctx.model.Olt.find({ tid: params.tid });
        console.log("查询到olt->", result.length);
        ctx.socket.emit("oltNums",result.length);
        for (var i = 0; i < result.length; i++) {
          try {
            let OltId = result[i]._id;
            let oltIP = result[i].oltIP;
            ctx.service.olt.getOltStatus({ OltId }).then((oltstate) => {
              if (oltstate.flag == true) {
                ctx.service.olt
                  .getRegisteredONUList({
                    OltId,
                    tid: params.tid,
                  })
                  .then((res) => {
                    ctx.socket.emit("oltRefreshResult", res.msg);
                  }).catch(err=>{
                    ctx.socket.emit("oltRefreshError", err);
                  });
              }else{
                ctx.socket.emit("oltRefreshResult",`${oltIP} offline`);
              }
            });
          } catch (error) {
            console.log(error);
            ctx.socket.emit("oltRefreshError", error);
          }
        }
      } else {
        try {
          ctx.socket.emit("oltNums",1);
          ctx.service.olt
            .getOltStatus({ OltId: params.oltId })
            .then((oltstate) => {
              if (oltstate.flag == true) {
                ctx.service.olt
                  .getRegisteredONUList({
                    OltId: params.oltId,
                    tid: params.tid,
                  })
                  .then((res) => {
                    ctx.socket.emit("oltRefreshResult", res.msg);
                  });
              }
            });
        } catch (error) {
          console.log(error);
          ctx.socket.emit("oltRefreshError", error);
        }
      }
    }
  }
  async fetchUnregONU(message) {
    const { ctx, app } = this;
    let reg = /.*oltId=(.*)&tid=(.*)&tenant=(.*)/gi;
    let group = reg.exec(message);
    if (group) {
      const [url, oltId, tid,tenant] = group;
      let params = {
        oltId,
        tid,
      };
      ctx.tenant = tenant;
      if (params.oltId == "Any") {
        const result = await ctx.model.Olt.find({ tid: params.tid });
        ctx.socket.emit("oltNums",result.length);
        for (var i = 0; i < result.length; i++) {
          let OltId = result[i]._id;
          let oltIP = result[i].oltIP;
          ctx.service.olt.getOltStatus({
            OltId
          }).then(oltstate=>{
            if(oltstate.flag==true){
              ctx.service.olt.getUnregisteredONUList({
                OltId
              }).then((res) => {
                ctx.socket.emit("oltRefreshResult", res.msg);
              });
            }else{
              ctx.socket.emit("oltRefreshResult",`${oltIP} offline`);
            }
          })
        }
      } else {
        ctx.socket.emit("oltNums",1);
        ctx.service.olt.getOltStatus({
          OltId: params.oltId,
        }).then(oltstate=>{
          if (oltstate.flag == true) {
            ctx.service.olt.getUnregisteredONUList({
              OltId: params.oltId,
            }).then((res) => {
              ctx.socket.emit("oltRefreshResult", res.msg);
            });
          }else{
            ctx.socket.emit("oltRefreshResult",`olt offline`);
          }
        })
      }
    }
  }
}

module.exports = DefaultController;
